//
//  LLMenuItem.m
//  lrlzapp
//
//  Created by dl on 16/2/25.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLMenuItem.h"

@implementation LLMenuItem

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
            @"uuid" : @"uuid",
            @"identifier" : @"type",
            @"name" : @"name",
            @"parentId" : @"parentId",
            @"url" : @"url",
            @"imgUrl" : @"imgUrl",
            @"desc" : @"description",
            @"status" : @"status",
            @"indexId" : @"indexId",
            @"createDate" : @"createDate"
    };
}

+ (NSValueTransformer *)statusJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)indexIdJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)parentIdJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)createDateJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLDateTimeValueTransformerName];
}

@end


@implementation LLMenuItemBag

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return [super.JSONKeyPathsByPropertyKey mtl_dictionaryByAddingEntriesFromDictionary:
            @{@"menuItems" : @"list"}];
}

+ (NSValueTransformer *)menuItemsJSONTransformer {
    return [MTLValueTransformer transformerUsingForwardBlock:^id(NSArray *jsonArray, BOOL *success, NSError *__autoreleasing *error) {
        NSError *localError = nil;
        NSArray *menuItems = [MTLJSONAdapter modelsOfClass:LLMenuItem.class fromJSONArray:jsonArray error:&localError];
        if (localError) {
            JSONArrayToModelArrayError(LLMenuItem.class, error, jsonArray);
            return nil;
        }
        return menuItems;
    }                                           reverseBlock:^id(NSArray *models, BOOL *success, NSError *__autoreleasing *error) {
        NSError *localError = nil;
        NSArray *jsonArray = [MTLJSONAdapter JSONArrayFromModels:models error:&localError];
        if (localError) {
            ModelArrayToJsonArrayError(LLMenuItem.class, error, models);
            return nil;
        }

        return jsonArray;
    }];
}

@end